<template>
  <div>
    <a-card class="">
      <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
        <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '80px' } }">
          <a-form-item name="searchWord">
            <a-input v-model:value="formState.searchWord" class="w-80" placeholder="姓名/助记码/手机号/申请单号/床位号" />
          </a-form-item>
          <a-form-item name="stdate" label="检查日期" class="w-[340px]">
            <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </a-form-item>
          <a-form-item name="categoryCode" label="医技分类" class="ml-2">
            <a-input v-model:value="formState.categoryCode" placeholder="请输入" />
          </a-form-item>
          <a-form-item class="ml-2">
            <a-radio-group v-model:value="formState.applyPath">
              <a-radio value="">全部</a-radio>
              <a-radio value="0">门诊</a-radio>
              <a-radio value="1">住院</a-radio>
              <a-radio value="2">体检</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-form>
      </SearchContainer>
    </a-card>

    <a-card class="mt-2">
      <div class="flex mb-2">
        <a-button class="mr-2" type="primary" @click="onEdit()"> 新增 </a-button>
      </div>
      <BaseTable :data-source="tableData" fill :loading="loading" :pagination="pagination" column-code="001095" :scroll="{ x: 2400 }">
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record.status === '未审核'" color="blue">未审核</a-tag>
            <a-tag v-if="record.status === '已审核'" color="green">已审核</a-tag>
            <a-tag v-if="record.status === '已打印'" color="green">已打印</a-tag>
            <a-tag v-if="record.status === '已作废'" color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'applyPath'">
            <span v-if="record.applyPath === 0">门诊</span>
            <span v-if="record.applyPath === 1">住院</span>
          </template>
          <template v-if="column.dataIndex === 'categoryName'">
            <PopoverTable :config="detailConfig" :params="{ data: record.appSn }" :width="500"> 测试 </PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'brName'">
            <div class="flex items-center">
              <div>{{ record?.brName }}</div>
              <div class="mx-4">{{ record?.brXb }}</div>
              <div>{{ record?.brAge }}</div>
            </div>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button
                type="link"
                :disabled="record.status === '已作废' || record.status === '已审核' || record.status === '已打印'"
                size="small"
                @click="onEdit(record)"
                >修改</a-button
              >
              <a-button
                v-if="record.status === '已作废'"
                :disabled="record.status === '已审核' || record.status === '已打印'"
                type="link"
                size="small"
                @click="onCancel(record)"
                >恢复</a-button
              >
              <BaseButton
                v-else
                confirm-text="确认作废?"
                :disabled="record.status === '已审核' || record.status === '已打印'"
                type="text"
                danger
                size="small"
                @click="onCancel(record)"
              >
                作废
              </BaseButton>
              <a-button
                type="link"
                :disabled="record.status === '已审核' || record.status === '已打印' || record.status === '已作废'"
                size="small"
                @click="onClickConfirm(record)"
                >确认</a-button
              >
              <a-button
                type="link"
                :disabled="record.status === '已作废' || (record.status !== '已审核' && record.status !== '已打印')"
                size="small"
                @click="onclickCode(record)"
                >条码</a-button
              >
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="open" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import Edit from './Edit.vue'

const rowData = ref({})
const formRef = ref(null)
const open = ref(false)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  applyPath: ''
})
const detailConfig = ref({
  url: '/ris/apply/getSfxmAndPartData',
  columnCode: '001096',
  rowKey: 'id',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      xh: index + 1
    }))
  }
})

const { loading, pagination, tableData, getData } = useTable({
  url: '/ris/apply/getApplyList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      startExamDate: formState.value.dateRange?.[0],
      endExamDate: formState.value.dateRange?.[1]
    }
  }
})

const onEdit = (row = {}) => {
  rowData.value = { ...row }
  open.value = true
}

// 作废-恢复
const onCancel = async (row) => {
  const { code, msg } = await post('/ris/apply/doChangeApplyStatus', { data: row.appSn })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}

// 确认
const onClickConfirm = async (row) => {
  const { code, msg } = await post('/ris/apply/doConfirmApply', { data: row.appSn })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  if (getSystemConfig('PRINT_RIS_APPLY_BARCODE_IN_CONFIRM') === '1') {
    onclickCode(row)
  }
  getData()
}

// 打印条码
const onclickCode = async (row) => {
  const { code, msg, data } = await post('/ris/apply/doPrintBarCode', { data: row.appSn })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
  getData()
}
</script>
